package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.exception.ApiException;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.modules.special.entity.AdminSpecialBindUserBuy;
import com.ksd.modules.special.mapper.AdminSpecialBindUserBuyMapper;
import com.ksd.modules.special.po.AdminSpecialBindUserBuySavePO;
import com.ksd.modules.special.po.AdminSpecialBindUserBuySearchPO;
import com.ksd.modules.special.service.IAdminSpecialBindService;
import com.ksd.modules.special.service.IAdminSpecialBindUserBuyService;
import com.ksd.modules.special.vo.AdminSpecialBindUserBuyVo;
import com.ksd.modules.special.vo.AdminSpecialBindVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialBindUserBuyServiceImpl extends BaseServiceImpl<AdminSpecialBindUserBuyMapper, AdminSpecialBindUserBuy> implements IAdminSpecialBindUserBuyService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminSpecialBindService adminSpecialService;

    /**
     * 查询列表
     *
     * @param adminSpecialBindUserBuySearchPO
     * @return
     */
    public Page<AdminSpecialBindUserBuyVo> findAdminSpecialBindUserBuys(AdminSpecialBindUserBuySearchPO adminSpecialBindUserBuySearchPO) {
        Page<AdminSpecialBindUserBuy> page = new Page<>(adminSpecialBindUserBuySearchPO.getPageNo(), adminSpecialBindUserBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecialBindUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialBindUserBuy::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminSpecialBindUserBuy::getSpecialId, adminSpecialBindUserBuySearchPO.getSpecialId());
        // 订单编号
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialBindUserBuySearchPO.getOrderno()), AdminSpecialBindUserBuy::getOrderno, adminSpecialBindUserBuySearchPO.getOrderno());
        // 支付方式
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialBindUserBuySearchPO.getPayMethod()), AdminSpecialBindUserBuy::getPayMethod, adminSpecialBindUserBuySearchPO.getPayMethod());
        // 具体资源订单
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialBindUserBuySearchPO.getOpid()), AdminSpecialBindUserBuy::getOpid, adminSpecialBindUserBuySearchPO.getOpid());
        // 兑换码
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialBindUserBuySearchPO.getCode()), AdminSpecialBindUserBuy::getCode, adminSpecialBindUserBuySearchPO.getCode());
        // uuid
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialBindUserBuySearchPO.getUuid()), AdminSpecialBindUserBuy::getUuid, adminSpecialBindUserBuySearchPO.getUuid());
        // 手机
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialBindUserBuySearchPO.getPhone()), AdminSpecialBindUserBuy::getPhone, adminSpecialBindUserBuySearchPO.getPhone());
        // 模糊匹配
        if (StringUtils.isNotEmpty(adminSpecialBindUserBuySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(
                            AdminSpecialBindUserBuy::getNickname, adminSpecialBindUserBuySearchPO.getKeyword())
                    .or().like(AdminSpecialBindUserBuy::getUsername, adminSpecialBindUserBuySearchPO.getKeyword())
                    .or().like(AdminSpecialBindUserBuy::getTitle, adminSpecialBindUserBuySearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminSpecialBindUserBuy::getCreateTime);
        Page<AdminSpecialBindUserBuy> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialBindUserBuyVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialBindUserBuyVo.class);
        return adminSpecialVoPage;
    }

    /**
     * 保存和更新
     *
     * @param adminSpecialBindUserBuySavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AdminSpecialBindUserBuyVo saveUpdateAdminSpecialBindUserBuy(AdminSpecialBindUserBuySavePO adminSpecialBindUserBuySavePO) {
        AdminSpecialBindUserBuy adminSpecial = tranferBo(adminSpecialBindUserBuySavePO, AdminSpecialBindUserBuy.class);
        adminSpecial.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecial.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminSpecial);
        AdminSpecialBindUserBuyVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialBindUserBuyVo.class);
        return adminSpecialVo;
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminSpecialBindUserBuyVo getAdminSpecialBindUserBuyById(Long id) {
        AdminSpecialBindUserBuy adminSpecial = this.getById(id);
        AdminSpecialBindUserBuyVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialBindUserBuyVo.class);
        return adminSpecialVo;
    }

    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminSpecialBindUserBuyById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     *
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemovesAdminSpecialBindUserBuy(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }

    /**
     * 用户绑定专题订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid) {
        LambdaQueryWrapper<AdminSpecialBindUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialBindUserBuy::getUuid, uuid);
        lambdaQueryWrapper.eq(AdminSpecialBindUserBuy::getOpid, opid);
        AdminSpecialBindUserBuy adminSpecialBindUserBuy = this.getOne(lambdaQueryWrapper);
        if (adminSpecialBindUserBuy != null) {
            throw new ApiException(501, "该用户已绑定此资源!!!");
        }
        adminSpecialBindUserBuy = new AdminSpecialBindUserBuy();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        AdminSpecialBindVo adminSpecialBindVo = adminSpecialService.getAdminSpecialBindById(opid);
        adminSpecialBindUserBuy.setOpid(opid);
        adminSpecialBindUserBuy.setSpecialId(adminSpecialBindVo.getSpecialId());
        adminSpecialBindUserBuy.setUserId(adminUserVo.getId());
        adminSpecialBindUserBuy.setNickname(adminUserVo.getNickName());
        adminSpecialBindUserBuy.setUsername(adminUserVo.getUserName());
        adminSpecialBindUserBuy.setAvatar(adminUserVo.getAvatar());
        adminSpecialBindUserBuy.setUuid(adminUserVo.getUuid());
        adminSpecialBindUserBuy.setPhone(adminUserVo.getTelephone());
        adminSpecialBindUserBuy.setAddress(adminUserVo.getAddress());

        adminSpecialBindUserBuy.setTitle(adminSpecialBindVo.getTitle());
        adminSpecialBindUserBuy.setDescription(adminSpecialBindVo.getDescription());
        adminSpecialBindUserBuy.setCover("");
        adminSpecialBindUserBuy.setCode("");
        adminSpecialBindUserBuy.setPrice(adminSpecialBindVo.getPrice());

        adminSpecialBindUserBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminSpecialBindUserBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminSpecialBindUserBuy.setPayMethod(4);
        adminSpecialBindUserBuy.setPayMethodName("人工");

        adminSpecialBindUserBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecialBindUserBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminSpecialBindUserBuy.setOrderJson("");
        return this.saveOrUpdate(adminSpecialBindUserBuy);
    }
}
